var loadCurrentPageInfo = function() {
	chrome.tabs.getSelected(null, function(tab) {

			});
};
var tabId;
var tempbookmark;
var createBookmarkEdited = function() {
	localStorage['last-url-input'] = $('url-input').value;
	localStorage['last-title-input'] = $('title-input').value;
	localStorage['last-notes-input'] = $('notes-input').value;
	localStorage['last-tags-input'] = $('tags-input').value;
	localStorage['last-send-input'] = $('send-input').value;
	$('url-input').removeClassName('unsaved');
	$('title-input').removeClassName('unsaved');
	$('notes-input').removeClassName('unsaved');
	$('tags-input').removeClassName('unsaved');
	$('send-input').removeClassName('unsaved');
	createBookmarkEditedJob.done();
}

var toggleOpened = function() {
	if (localStorage['opened']) {
		delete localStorage['opened'];
	} else {
		localStorage['opened'] = 'yes';
	}
}

var createBookmark = function() {
	tempbookmark = {
		name : $('title-input').value,
		url : $('url-input').value,
		urlMD5 : MD5($('url-input').value),
		description : $('notes-input').value,
		tags : $('tags-input').value.split(' ')
	};
	bookmarkStorage.updateBookmark(tempbookmark);
	window.close();
}
var createBookmarkEditedJob;

var schedCreateBookmarkEdited = function(id) {
	createBookmarkEditedJob.replaceSchedule();
	$(id).addClassName('unsaved');
}

window.addEventListener('load', function() {

	try {
		chrome.tabs.getSelected(null, function(tab) {
					$('url-input').value = tab.url;
					$('title-input').value = tab.title
							|| 'Not loaded. Please Wait...';
					tempbookmark = {
						name : $('title-input').value,
						url : $('url-input').value,
						urlMD5 : MD5($('url-input').value)
					};
					tabId = tab.id;
					if (tab.url == localStorage['last-url-input']) {
						$('url-input').value = localStorage['last-url-input'];
						$('title-input').value = localStorage['last-title-input']
								|| 'Not loaded. Please Wait...';;
						$('notes-input').value = localStorage['last-notes-input'];
						$('tags-input').value = localStorage['last-tags-input'];
						$('send-input').value = localStorage['last-send-input'];
						if (localStorage['opened']) {
							setTimeout(function() {
										openPanel('create-bookmark');
										closePanel('bookmarks')
									}, 100);
						}
					} else {
						localStorage['last-url-input'] = tab.url;
						localStorage['last-title-input'] = tab.title;
						delete localStorage['opened'];
					}
				});

		createBookmarkEditedJob = new Job(createBookmarkEdited, 100);
		loadResults();
		chrome.extension.getBackgroundPage().canvasRotater.animateFlip();
		$$('.hide').each(function(el) {
					el.removeClassName('hide');
				});

	} catch (e) {
		console.dir(e.stack || e);
	}
}, false);